using System;
using System.Collections;
using System.Diagnostics;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.Common;
using TXLooker.Common.Core;

namespace TXLooker.WorkItemExtention.Core
{
    internal class WIProjectProvider : WIObject
    {
        protected IProjectProvider Source
        {
            get { return this.m_source; }
            private set
            {
                this.m_source = value;
                this.Folder = this.m_source.Folder;
            }
        }
        private IProjectProvider m_source;

        public WIProjectProvider(WIServerProvider parent, IProjectProvider source)
            : base(parent)
        {
            Settings = new WIProjectSettings();
            this.Source = source;

            this.QueriesRoot = new WIQueriesRoot(this);
            this.Childs.Add("QueriesRoot", QueriesRoot);

            try
            {
                this.Settings.Init(this.GetProject());
            }
            catch (Exception ex)
            {
                LogManager.WriteMessageToCustomLog("Error WIProjectProvider constructor " + ex.Message);
            }
        }

        public WIServerProvider ServerProvider
        {
            get { return (this.Parent as WIServerProvider); }
        }
        public Uri ProjectUri
        {
            get { return this.Source.ProjectUri; }
        }
        public string ProjectName
        {
            get { return this.Source.ProjectName; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override WIObjectType Type
        {
            get { return WIObjectType.Project; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Project GetProject()
        {
            Project project = null;

            //  try
            {
                WorkItemStore store = this.GetStore();

                if (store != null)
                {
                    ICollection projects = store.Projects;

                    lock (projects.SyncRoot)
                    {
                        foreach (Project item in projects)
                        {
                            if (item.Uri != this.ProjectUri) continue;
                            project = item;
                            break;
                        }
                    }
                }
            }
            // catch( System.Exception ex )
            {
                //Debug.WriteLine( ex.ToString() );
                //TeamXpand.Common.LogManager.WriteMessageToCustomLog("Error (GetProject) WIProjectProvider " + ex.Message);

            }

            return project;
        }

        public WIQueriesRoot QueriesRoot { get; private set; }

        protected override void UpdateData()
        {
            lock (this.Childs)
            {
                foreach (WIObject item in this.Childs.Values)
                {
                    try
                    {
                        item.Update();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.ToString());
                        LogManager.WriteMessageToCustomLog("Error (UpdateData) WIProjectProvider " + ex.Message);

                    }

                }
            }
        }

        public override string FolderName
        {
            //!!!!we do not create folder! Folder is created yet before instance init!!!!
            get { return this.Folder.Name; }
        }

        public override OlFolderView FolderView
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public WIProjectSettings Settings { get; private set; }
    }
}

